package controller;


import org.apache.ibatis.jdbc.Null;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import service.IUerService;
import until.Encrypt;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;


public class LoginController {
    @Resource
    private IUerService iUerService;

    @RequestMapping("login")
    public String toLogin(HttpSession httpSession) {
       String usernamne=(String) httpSession.getAttribute("username");
       String passwordByMd5=(String) httpSession.getAttribute("passwordByMd5");
       if(usernamne!=null&&passwordByMd5!=null){
           return  "home";
       }
       else {
           return "login";
       }
    }


    /*验证登录
     *
     * 使用 @ResponseBody 在ajax异步获取数据时使用。返回0/1
     *
     */
    @RequestMapping(value = "login_check", method = RequestMethod.POST)
    @ResponseBody
    public String login(String username, String password, HttpSession httpSession) throws Exception{
        String passwordByMd5 = Encrypt.MD5(password);
        String megs;
        int i = iUerService.login("username", "passwordByMd5");
        if(i==1){
            //查询到一条记录，证明该用户合法
            System.out.println("登录成功！");
            int id=iUerService.setSession("username", "passwordByMd5");
               httpSession.setAttribute("id","id");
               httpSession.setAttribute("username","username");
               httpSession.setAttribute("passwordByMd5","passwordByMd5");
            return "home";
        }else {
            return "login";
        }

    }


}